import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryException;
import com.ice.jni.registry.RegistryKey;
import com.ice.jni.registry.RegistryValue;

import java.util.Enumeration;

/**
 * Created by patrick on 6/9/2017.
 */
public class Reg {

    private static String profileImagePath = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";

    private static String profileImage = "S-1-5-21-";

    private static String userShellFoldersPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer";

    private static String userShellFolders = "User Shell Folders";

    private static String shellFoldersPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer";

    private static String shellFolders = "Shell Folders";



    private static String diskC = "C:\\Users\\patrick";

    private static String userProfile = "%USERPROFILE%";

    private static String diskD = "D:\\Users\\patrick";

    public static void main(String[] args) throws Exception {
        updateRegistryKey(Registry.HKEY_CURRENT_USER, userShellFoldersPath, userProfile, userShellFolders);
//        updateRegistryKey(Registry.HKEY_CURRENT_USER, shellFoldersPath, diskC, shellFolders);
//        updateRegistryKey(Registry.HKEY_LOCAL_MACHINE, profileImagePath, diskC, profileImage);

    }

    private static void updateRegistryKey(RegistryKey topKey, String pathKey,String beforeValue, String name) throws RegistryException {
        String nodeName = name;
        RegistryKey registryKey = Registry.openSubkey(topKey, pathKey, RegistryKey.ACCESS_READ);
        if(nodeName.startsWith(profileImage)){
            Enumeration temp = registryKey.keyElements();
            while (temp.hasMoreElements()){
                String tempKey = temp.nextElement().toString();
                if(tempKey.startsWith(profileImage)){
                    nodeName = tempKey;
                    break;
                }
            }
        }
        RegistryKey subRegistryKey = registryKey.openSubKey(nodeName, RegistryKey.ACCESS_ALL);
        Enumeration enumeration = subRegistryKey.valueElements();
        while (enumeration.hasMoreElements()) {
            String key = enumeration.nextElement().toString();
            if(key.indexOf('!') == 0){
                continue;
            }

            String value = subRegistryKey.getStringValue(key);
            String data = value.replace(beforeValue, diskD);

            if(value.equals(data)){
                continue;
            }

            System.out.print("[" + key + ":" + value +"]=====");
            System.out.println("[" + key + ":" + data +"]");

            RegistryValue registryValue = subRegistryKey.getValue(key);
            registryValue.setByteData(data.getBytes());
            subRegistryKey.setValue(registryValue);

        }
        subRegistryKey.flushKey();
        subRegistryKey.closeKey();
        registryKey.flushKey();
        registryKey.closeKey();

    }

}
